package com.example.userdome.Interceptor;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.userdome.pojo.User;
import com.example.userdome.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Resource
    private JwtUtils jwtUtils;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String h1=request.getHeader("Authorization");
        if (h1==null){
            return false;
        }

        Claims claims=jwtUtils.parseToken(h1);
        if (claims==null){
            return false;
        }
        request.setAttribute("user",Long.parseLong(claims.getSubject()));
        return true;

    }
}
